import api from '@/api';

export default {
  namespace: 'notification',

  state: {
    notifications: {
      list: [],
      pagination: {},
    },
    companyList: {
      list: [],
      pagination: {},
    },
    currentNotification: {},
    types: [],
    status: [],
    targets: [],
    departments: [],
    currentStatus: '1',
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const response = yield call(api.notification.query, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveNotifications',
          payload: {
            list: response.data,
            pagination: response.pagination,
          },
        });
      }
      return response;
    },
    *fetchCurrent({ payload }, { call, put }) {
      const response = yield call(api.notification.queryCurrent, payload);
      if (response.errorCode === 0) {
        yield put({
          type: 'saveCurrent',
          payload: response.data,
        });
      }
      return response;
    },
    *delete({ payload }, { call }) {
      return yield call(api.notification.remove, payload);
    },
    *add({ payload }, { call }) {
      return yield call(api.notification.create, payload);
    },
    *update({ payload }, { call }) {
      return yield call(api.notification.update, payload);
    },
    *submitAudit({ payload }, { call }) {
      return yield call(api.notification.submitAudit, payload);
    },
    *audit({ payload }, { call }) {
      return yield call(api.notification.audit, payload);
    },
    *cancelAudit({ payload }, { call }) {
      return yield call(api.notification.cancelAudit, payload);
    },
    *fetchTypes({ payload }, { call, put }) {
      let res = yield call(api.notification.queryNotificationTypes, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveTypes',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchStatus({ payload }, { call, put }) {
      let res = yield call(api.notification.queryNotificationStatus, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveStatus',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchTargets({ payload }, { call, put }) {
      let res = yield call(api.notification.queryNotificationTargets, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveTargets',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchDepartments(_, { call, put }) {
      let res = yield call(api.notification.departments);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveDepartments',
          payload: res.data,
        });
      }
      return res;
    },
    *fetchTargetUser({ payload }, { call }) {
      const res = yield call(api.notification.targetUser, payload);
      return res;
    },
    *fetchPublishers({ payload }, { call }) {
      const res = yield call(api.notification.publisher, payload);
      return res;
    },
    *fetchCompanyList({ payload }, { call, put }) {
      const res = yield call(api.notification.companyList, payload);
      if (res.errorCode === 0) {
        yield put({
          type: 'saveCompanyList',
          payload: {
            list: res.data,
            pagination: res.pagination,
          },
        });
      }
      return res;
    },
    *fetchMarkList({ payload }, { call }) {
      const url = payload.queryParams.msgId ? api.notification.companyStudy : api.notification.platformStudy;
      return yield call(url, payload);
    },
  },

  reducers: {
    updateTabStatus(state, { payload }) {
      return {
        ...state,
        currentStatus: payload,
      };
    },
    saveNotifications(state, { payload }) {
      return {
        ...state,
        notifications: payload,
      };
    },
    saveCompanyList(state, { payload }) {
      return {
        ...state,
        companyList: payload,
      };
    },
    saveCurrent(state, { payload }) {
      return {
        ...state,
        currentNotification: payload,
      };
    },
    clearCurrent(state) {
      return {
        ...state,
        currentNotification: {},
      };
    },
    saveTypes(state, { payload }) {
      return {
        ...state,
        types: payload,
      };
    },
    saveStatus(state, { payload }) {
      return {
        ...state,
        status: payload,
      };
    },
    saveTargets(state, { payload }) {
      return {
        ...state,
        targets: payload,
      };
    },
    saveDepartments(state, { payload }) {
      return {
        ...state,
        departments: payload,
      }
    },
  },
};
